package com.scm.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import scm.classifica.jaxb.CLSBean;
import scm.classifica.jaxb.HomeResult;
import scm.classifica.jaxb.OutDoorResult;
import scm.classifica.jaxb.Team;
import scm.classifica.jaxb.TotalResult;
import com.scm.error.SCPException;
import com.scm.property.PropertyConfig;
import com.scm.property.IPropertyConfig;
import com.scm.utils.Codes;


public class TEXTParserStatistic implements StatisticParser{
	
	private Logger log = Logger.getLogger(TEXTParserStatistic.class);
	
	private String urlHtml = null;
	private CLSBean teamObject = null;
	private String CAMPIONATO = null;
	
	public TEXTParserStatistic(String campionato) {
		 
		 IPropertyConfig property = new PropertyConfig();
		 this.urlHtml = property.getStringUrlClass(Codes.TIPO_CLASSIFICA, campionato);
		 teamObject =  new CLSBean();
		 teamObject.setCampionato(campionato);
		 teamObject.setNazione(campionato);
		 this.CAMPIONATO = campionato;
		 log.debug("Url: "+urlHtml);
	}
	
	public CLSBean getBeanStatistic() throws SCPException  {
		
		URL url;
		List<String> classifica = new ArrayList<String>();
		try {
			url = new URL(urlHtml);
			URLConnection yc = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine;
			
		while ((inputLine = in.readLine()) != null) {
			if (inputLine.contains("class=Tabella")) {
				String test = inputLine.replaceAll("<[^>]*>", "");
				test = test.replaceAll("\\s+", " ");
				String[] arr = test.split("&nbsp;");
				classifica = Arrays.asList(arr);
			}
		}
		in.close();

		} catch (MalformedURLException e) {
			throw new SCPException("URL_DWNLOAD_PDF_FAILED",665,"URL Download "+urlHtml+" Non valido!");
		} catch (IOException e) {
			throw new SCPException("DWNLOAD_PDF_FAILED",666,"Download Pdf Fallito!");
		}
		
		
		for (String cls : classifica.subList(1, classifica.size())){
			Team team = new Team();
			String[] temp = cls.split(" ");
			int index = 0;
			int lengthName = temp.length - 17;
			String nameTeam ="";
			for(index = 0 ; index < lengthName; index++ ){
				nameTeam = nameTeam + temp[index]+" ";
			}
		    team.setNameTeam(nameTeam);
		    team.setCmp(CAMPIONATO);
		    
		    TotalResult tr = new TotalResult();
		    tr.setPuntiTotali(temp[index]);
		    tr.setPartiteGiocate(temp[index+1]);
		    tr.setVinte(temp[index+2]);
		    tr.setNulle(temp[index+3]);
		    tr.setPerse(temp[index+4]);
		    tr.setRfatti(temp[index+5]);
		    tr.setRsubite(temp[index+6]);
		    team.setTotalResult(tr);
		    
		    HomeResult hr = new HomeResult();
		    hr.setVinte(temp[index+7]);
		    hr.setNulle(temp[index+8]);
		    hr.setPerse(temp[index+9]);
		    hr.setRfatti(temp[index+10]);
		    hr.setRsubite(temp[index+11]);
		    team.setHomeResult(hr);
		    
		    OutDoorResult odr = new OutDoorResult();
		    odr.setVinte(temp[index+12]);
		    odr.setNulle(temp[index+13]);
		    odr.setPerse(temp[index+14]);
		    odr.setRfatti(temp[index+15]);
		    odr.setRsubite(temp[index+16]);
		    team.setOutDoorResult(odr);
		    
		    teamObject.getTeam().add(team);
		}
		
		return teamObject;
	}

}
